package com.wanda.controller;

import com.wanda.common.R;
import com.wanda.common.ResponseEnum;
import com.wanda.entity.*;
import com.wanda.service.AdminService;
import com.wanda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author sky
 * @date 2022-08-26
 */
@RestController
@RequestMapping("/wanda")
@CrossOrigin("*")
public class CinemaController {
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询当前日期的热门影片
     * @param date
     * @return
     */
    @GetMapping("/movies")
    public R selectMoviesByDate(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date date){
        List<Movie> movies = userService.selectMoviesByDate(date);
        System.out.println(movies);
        return new R(ResponseEnum.SUCCESS,movies);
    }

    /**
     * 查询电影院
     * @param cityName
     * @return
     */
    @GetMapping("/cinemas")
    public R selectCinemaByCityidAndMovieid(String cityName,Long movieId){
        List<Cinema> cinemas = userService.selectCinemaByCityidAndMovieid(cityName, movieId);
        System.out.println(cinemas);
        return new R(ResponseEnum.SUCCESS,cinemas);
    }

    /**
     * 查询场次
     * @param cinemaId
     * @param movieId
     * @param date
     * @return
     */
    @GetMapping("/screens")
    public R selectScreenByExample(Long cinemaId, Long movieId, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date date){
        List<Screen> screens = userService.selectScreenByExample(cinemaId, movieId, date);
        System.out.println(screens);
        return new R(ResponseEnum.SUCCESS,screens);
    }


    /**
     * 获取座位表
     * @param screenId
     * @return
     */
    @GetMapping("/sites")
    public R selectSiteByScreenId(Long screenId){
        List<Site> sites = adminService.selectSeat(screenId);
        if (sites == null || sites.size() == 0){
            adminService.CreateSeat(screenId);
            List<Site> newSites = adminService.selectSeat(screenId);
            return new R(ResponseEnum.SUCCESS,newSites);
        }
        System.out.println(sites);
        return new R(ResponseEnum.SUCCESS,sites);
    }

    /**
     * 获取票表
     */
    @GetMapping("/tickets")
    public R selectTicketByScreenId(Long screenId){
        List<Ticket> tickets = userService.selectTicketByScreenId(screenId);
        return new R(ResponseEnum.SUCCESS,tickets);
    }

    /**
     * 买票
     */
    @PostMapping("/buyTicket")
    public R buyTicket(Long screenId,@RequestParam List<String> list){
        System.out.println(list);
        System.out.println("list:" + list);
        userService.updateTickets(screenId,list);
        return new R(ResponseEnum.SUCCESS,list);
    }
}
